package code4;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    public static void main(String[] args) {

        Socket s = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        Scanner sc = null;

        try {
            s = new Socket("127.0.0.1", 8888);
            System.out.println("连接服务器成功！");

            sc = new Scanner(System.in);

            oos = new ObjectOutputStream(s.getOutputStream());
            ois = new ObjectInputStream(s.getInputStream());

            while(true){
                System.out.println("请输入用户名和密码（用户名，密码）：");
                String str = sc.next();
                String[] sss = str.split(",");
                UserMessage um = new UserMessage("check", new User(sss[0], sss[1]));
                oos.writeObject(um);
                System.out.println("客户端发送数据成功！");
                UserMessage u = (UserMessage)ois.readObject();
                if("success".equals(u.getUserClass())){
                    System.out.println("登录成功！");
                    break;
                }else{
                    System.out.println("登录失败！");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(null!=oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=ois){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=s){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=sc){
                sc.close();
            }
        }
    }
}
